package com.wpc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wpc.model.Product;

public class ProductDAOImpl extends BaseDAO implements ProductDAO {

	private static final String GET_PRODUCTS_SQL = "SELECT name,description,price FROM products";
	private static final String INSERT_PRODUCT_SQL = "INSERT INTO products (name,description,price) VALUES (?,?,?)";

	@Override
	public List<Product> getProducts() throws DAOException {
		List<Product> products = new ArrayList<>();
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;

		try {
			connection = getConnection();
			statement = connection.prepareStatement(GET_PRODUCTS_SQL);
			resultSet = statement.executeQuery();

			while (resultSet.next()) {
				Product product = new Product();
				product.setName(resultSet.getString("name"));
				product.setDescription(resultSet.getString("description"));
				product.setPrice(resultSet.getFloat("price"));
				products.add(product);
			}

		} catch (SQLException e) {
			throw new DAOException("获得产品失败！" + e.getMessage());
		}
		return products;
	}

	@Override
	public void insertProduct(Product product) throws DAOException {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(INSERT_PRODUCT_SQL);

			preparedStatement.setString(1, product.getName());
			preparedStatement.setString(2, product.getDescription());
			preparedStatement.setFloat(3, product.getPrice());

			preparedStatement.execute();
		} catch (SQLException e) {
			throw new DAOException("添加失败！" + e.getMessage());
		} finally {
			closeDBObjects(null, preparedStatement, connection);
		}

	}

}
